package com.hsy.linemoney.common;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.hsy.linemoney.R;
import com.deparse.dpframe.ui.android.widget.MaterialProgressBar.CircleProgressBar;

/**
 * Created by MartinKent on 2016/8/21.
 */
public class LoadingProgressDialog extends Dialog {
    private View contentView;
    private CircleProgressBar progressBar;
    private boolean mCancelable = false;
    private boolean mCanceledOnTouchOutside = false;

    public LoadingProgressDialog(Context context) {
        super(context, R.style.loading_progress_dialog);
        init(context);
    }

    public LoadingProgressDialog(Context context, int themeResId) {
        super(context, R.style.loading_progress_dialog);
        init(context);
    }

    protected LoadingProgressDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, R.style.loading_progress_dialog);
        init(context);
    }

    @Override
    public void setCancelable(boolean flag) {
        super.setCancelable(flag);
        this.mCancelable = flag;
    }

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
        this.mCanceledOnTouchOutside = cancel;
    }

    private void init(Context context) {
        contentView = LayoutInflater.from(context).inflate(R.layout.my_progress_dialog, null);
        setContentView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        progressBar = (CircleProgressBar) contentView.findViewById(R.id.progressWithoutBg);
        progressBar.setColorSchemeResources(android.R.color.holo_red_light);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mCancelable) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
}
